
//
//  WJSearchCell.m
//  WJBSApp
//
//  Created by dllo on 16/7/15.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJSearchCell.h"
#import "WJLastModel.h"
#import "WJDataManager.h"

@interface WJSearchCell ()

@property (nonatomic,retain) WJBaseImageView *headerImageView;

@property (nonatomic,retain) WJBaseLable *fans_countLable;

@property (nonatomic,retain) WJBaseLable *tiezi_countLable;

//@property (nonatomic,retain) WJBaseLable *comment_countLable;

@property (nonatomic,retain) UILabel *belowLable;

@end

@implementation WJSearchCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.headerImageView = [[WJBaseImageView alloc]init];
        [self.contentView addSubview:self.headerImageView];
        
        self.nameLable = [[WJBaseLable alloc]init];
        [self.contentView addSubview:self.nameLable];
        
        self.fans_countLable = [[WJBaseLable alloc]init];
        [self.contentView addSubview:self.fans_countLable];
        
        self.tiezi_countLable = [[WJBaseLable alloc]init];
        [self.contentView addSubview:self.tiezi_countLable];
        
//        self.comment_countLable = [[WJBaseLable alloc]init];
//        [self.contentView addSubview:self.comment_countLable];
 
//#warning +关注 需要判断啊  在数据库里面  要是有就显示 取消关注
        self.gzButton = [WJBaseButton buttonWithType:UIButtonTypeSystem];
        [self.contentView addSubview:self.gzButton];
        [self.gzButton addTarget:self action:@selector(addFolow:) forControlEvents:1<<6];
       
        
        self.belowLable = [[UILabel alloc]init];
        [self.contentView addSubview:self.belowLable];
        self.belowLable.backgroundColor = BSGrayColor;
        
        
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.headerImageView.frame = CGRectMake(10, 10, HEIGHT / 9 - 20,HEIGHT / 9 - 20);
    self.headerImageView.layer.cornerRadius = (HEIGHT / 9 - 20) / 2;
    self.headerImageView.layer.masksToBounds = YES;
    
    self.nameLable.frame = CGRectMake(15 +  HEIGHT / 9 - 20, 10, 200, (HEIGHT / 9 - 20) / 2);
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
    CGRect rect = [self.fans_countLable.text boundingRectWithSize:CGSizeMake(1000, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.fans_countLable.frame = CGRectMake(15 +  HEIGHT / 9 - 20, HEIGHT / 18, rect.size.width + 20, (HEIGHT / 9- 20) / 2 - 1);
    self.fans_countLable.font = [UIFont systemFontOfSize:14];
    
    CGRect rect1 = [self.tiezi_countLable.text boundingRectWithSize:CGSizeMake(1000, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.tiezi_countLable.frame = CGRectMake(15 +  HEIGHT / 9 - 20 +  rect.size.width + 20, HEIGHT / 18, rect1.size.width + 20 , (HEIGHT / 9- 20) / 2 - 1);
    self.tiezi_countLable.font = [UIFont systemFontOfSize:14];
    
    
//    CGRect rect2 = [self.tiezi_countLable.text boundingRectWithSize:CGSizeMake(1000, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
//    self.comment_countLable.frame = CGRectMake(15 +  HEIGHT / 9 - 20 +  rect.size.width + 20 + rect1.size.width + 20 , HEIGHT / 18, rect2.size.width + 20 , (HEIGHT / 9- 20) / 2 - 1);
//    self.comment_countLable.font = [UIFont systemFontOfSize:14];
    
    
    self.gzButton.frame = CGRectMake(WIDTH * 6 / 7 - 10, (HEIGHT / 9 - 30) / 2, WIDTH / 7, 30);
    self.belowLable.frame = CGRectMake(0, HEIGHT/9 - 1, WIDTH , 1);
    
}

- (void)setModel:(WJLastModel *)model
{
    _model = model;
    [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:model.profile_image]];
    
    self.nameLable.text = model.username;
    
    self.fans_countLable.text = [NSString stringWithFormat:@"%@人关注",model.follow_count];
    
    self.tiezi_countLable.text = [NSString stringWithFormat:@"%ld个帖子",(long)model.tiezi_count];
    
//    self.comment_countLable.text = [NSString stringWithFormat:@"%@评论",model.comment_count];
    
    BOOL isExist = [[WJDataManager shareFMDB] isExistUserID:model.id];
    
    if (isExist) { //也就是存在
        self.gzButton.titleLabel.font = [UIFont systemFontOfSize:11];
        [self.gzButton setTitle:@"取消关注" forState:0];
        [self.gzButton setTitleColor:BSGrayColor forState:0];
        
    }else{  //也就是不存在
        self.gzButton.titleLabel.font = [UIFont systemFontOfSize:17];
        [self.gzButton setTitleColor:BSRedColor forState:0];
        [self.gzButton setTitle:@"+关注" forState:0];
    }
}
- (void)addFolow:(WJBaseButton *)button
{
    // ID 和Name
    NSLog(@"添加数据库啊啊啊啊啊啊啊啊啊啊啊啊");
    NSString *string = button.titleLabel.text;
    if ([string isEqualToString:@"取消关注"]) { // 执行取消关注 也就是删除
        self.gzButton.titleLabel.font = [UIFont systemFontOfSize:17];
        [button setTitle:@"+关注" forState:0];
        [button setTitleColor:BSRedColor forState:0];
        [[WJDataManager shareFMDB] deleteWith:self.model.id];
        
    }else{
        self.gzButton.titleLabel.font = [UIFont systemFontOfSize:11];
        [button setTitle:@"取消关注" forState:0];
        [button setTitleColor:BSGrayColor forState:0];
        [[WJDataManager shareFMDB] insertWithID:self.model.id username:self.model.username tiezi_count:self.model.tiezi_count follow_count:self.model.follow_count profile_image:self.model.profile_image];
    }
    
}







@end
